import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2024-03-04
 * Time: 12:49
 */
class User {
    public String username;
    public String password;
}
@WebServlet("/Parameter3")
public class Parameter3_servlet extends HttpServlet {
//    {
//        username = "zhangsan",
//        password = "123"
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        User user = objectMapper.readValue(req.getInputStream(),User.class);
        System.out.println("username = " + user.username + "  password = " + user.password);


        //jackson 还提供了一种方法，能够把 java 对象，转成 json 字符串
        String UserString = objectMapper.writeValueAsString(user);
        System.out.println("UserString:" + UserString);

        resp.getWriter().write("okk" );

    }
}
